/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ing.sisval.gui.control;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.ing.sisval.bl.BLDistribucion;
import org.ing.sisval.bl.BLEspecificacionTecnica;
import org.ing.sisval.gui.JFPrincipal;
import org.ing.sisval.gui.control.model.ModeloEspecificacionTecnica;
import static org.rx.cr.util.Utilitarios.addEnterDoclickEvent;
import static org.rx.cr.util.Utilitarios.alinearDatosColumnaTablaCentro;
import static org.rx.cr.util.Utilitarios.formateaAnchoColumnaTabla;

/**
 *
 * @author Mysham
 */
public class JIFAsignarEspecificaciónTecnica extends javax.swing.JInternalFrame {
  JFPrincipal parent;
    ModeloEspecificacionTecnica modeloEspecificacionTecnica;
    BLEspecificacionTecnica blEspecificacionTecnica;
  
    /**
     * Creates new form JIFAdministrarDistribucion
     */
    public JIFAsignarEspecificaciónTecnica(JFPrincipal parent) {
        initComponents();
        this.parent = parent;
        modeloEspecificacionTecnica= new ModeloEspecificacionTecnica();
        jtEspecificacionTecnica.setModel(modeloEspecificacionTecnica);
        personalizaTabla();
        blEspecificacionTecnica= new BLEspecificacionTecnica(parent.getConf());
         try {
            listarTabla();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al listar la lista de valores complementarios." + e, "Atención.", JOptionPane.INFORMATION_MESSAGE);

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtEspecificacionTecnica = new javax.swing.JTable();
        txfBuscar = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jbAgregar = new javax.swing.JButton();
        jbCargar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("DISTRIBUCIÓN");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DISTRIBUCIÓN", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jtEspecificacionTecnica.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jtEspecificacionTecnica.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jtEspecificacionTecnicaMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jtEspecificacionTecnica);

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/small_buscar.png"))); // NOI18N
        jbBuscar.setBorder(null);
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });

        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/small_agregar.png"))); // NOI18N
        jbAgregar.setBorder(null);
        jbAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txfBuscar)
                        .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jbCargar.setText("CARGAR");
        jbCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbCargar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCargar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtEspecificacionTecnicaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtEspecificacionTecnicaMouseReleased
        // TODO add your handling code here:
        jbCargar.setEnabled(true);
    }//GEN-LAST:event_jtEspecificacionTecnicaMouseReleased

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
        try {
            listarTabla();
        } catch (Exception ex) {
            Logger.getLogger(JIFAsignarEspecificaciónTecnica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jbCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCargarActionPerformed
        // TODO add your handling code here:

        if (jtEspecificacionTecnica.getSelectedRow() >= 0) {
           parent.administrarPropiedad.agregarEspecificacionTecnica(modeloEspecificacionTecnica.get(jtEspecificacionTecnica.getSelectedRow()));
        }

    }//GEN-LAST:event_jbCargarActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            listarTabla();
        } catch (Exception ex) {
            Logger.getLogger(JIFAsignarEspecificaciónTecnica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formComponentShown

    private void jbAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAgregarActionPerformed
        // TODO add your handling code here:
        parent.insertarInternalFrame(parent.jifEspecificacionTecnica);
    }//GEN-LAST:event_jbAgregarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCargar;
    private javax.swing.JTable jtEspecificacionTecnica;
    private javax.swing.JTextField txfBuscar;
    // End of variables declaration//GEN-END:variables
 public void _agregaValidadoresControl() {
        addEnterDoclickEvent(new Object[][]{{txfBuscar, jbBuscar}});
    }
    public void listarTabla() throws Exception {
        try {
            modeloEspecificacionTecnica.addAll(blEspecificacionTecnica.buscarReferencia(txfBuscar.getText()));
            personalizaTabla();
            jbCargar.setEnabled(false);
        } catch (Exception e) {
            Logger.getLogger(JIFAsignarEspecificaciónTecnica.class.getName()).log(Level.SEVERE, null, e);
        }

    }
 private void personalizaTabla() {
        alinearDatosColumnaTablaCentro(0, jtEspecificacionTecnica);
        formateaAnchoColumnaTabla(0, jtEspecificacionTecnica, 229);
    }
}
